//
//  ChangeView.m
//  weiGuanJia
//
//  Created by吕飞俊 on .
//  Copyright (c) 年 吕飞俊. All rights reserved.
//

#import "lvfjSegmentView.h"
#import "lvfjCommon.h"
#import "UIView+lvfjFrame.h"
#import "UIColor+lvfjHelper.h"

@interface lvfjSegmentView ()

/**选中标题颜色*/
@property (nonatomic, strong) UIColor *titleSeletedColor;
/**未中标题颜色*/
@property (nonatomic, strong) UIColor *titleNormalColor;

/**头*/
@property (nonatomic, strong) UIView *titleView;

/**标题数组*/
@property (nonatomic, strong) NSArray *tittleArray;

/**选中的线*/
@property (nonatomic, strong) UIView *selectedLine;

/**按钮数组*/
@property (nonatomic, strong) NSMutableArray *btArray;
/**选择的标题按钮*/
@property (nonatomic, strong) UIButton *seletBt;
/**按钮宽度*/
@property (nonatomic, assign) CGFloat btWidth;

/**视图数组*/
@property (nonatomic, strong) NSArray *viewArray;
/**控制器数组*/
@property (nonatomic, strong) NSArray *ctlArray;

/**滑动部分*/
@property(nonatomic,strong)UIScrollView *scrollView;

@end

@implementation lvfjSegmentView

-(instancetype)initWithFrame:(CGRect)frame
                  titleArray:(NSArray *)titleArray
                 titleHeight:(CGFloat)titleHeight
                   titleFont:(UIFont *)titleFont
            titleNormalColor:(UIColor *)titleNormalColor
           titleSeletedColor:(UIColor *)titleSeletedColor
                   lineColor:(UIColor *)lineColor
                  lineHeight:(CGFloat)lineHeight
                    ctlArray:(NSArray *)ctlArray
                    delegate:(id<ChangeViewDelegate>)delegate{
    
    if (self = [super initWithFrame:frame]) {
        //头
        self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, titleHeight)];
        self.titleView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.titleView];
        
        self.tittleArray = titleArray;
        self.titleSeletedColor = titleSeletedColor;
        self.titleNormalColor = titleNormalColor;
        
        for (UIButton *btn in self.btArray){
            [btn removeFromSuperview];
        }
        /**标题按钮数组*/
        self.btArray = [[NSMutableArray alloc] init];
        /**标题宽度-----屏幕宽度/标题个数*/
        self.btWidth = ScreenWidth/self.tittleArray.count;
        
        for (int i = 0; i<self.tittleArray.count; i++){
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(i%self.tittleArray.count*self.btWidth, 0, self.btWidth, titleHeight);
            [btn setTitle:self.tittleArray[i] forState:UIControlStateNormal];
            btn.titleLabel.font = titleFont;
//        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            btn.backgroundColor = [UIColor clearColor];
            btn.tag = i;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            if (i == 0){
                self.seletBt = btn;
                [btn setTitleColor:self.titleSeletedColor forState:UIControlStateNormal];
            }else{
                [btn setTitleColor:self.titleNormalColor forState:UIControlStateNormal];
            }
            [self.titleView addSubview:btn];
            
            [self.btArray addObject:btn];
            
            //按钮分隔线
            //        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(i*floatBtnWidth_-1, 10, 1, 21)];
            //        viewLine.backgroundColor = DCOLOR_LINE;
            //        [self addSubview:viewLine];
        }
        
        //选中线
        self.selectedLine = [[UIView alloc] initWithFrame:CGRectMake(0, titleHeight - lineHeight, self.btWidth, lineHeight)];
        self.selectedLine.backgroundColor = lineColor;
        [self.titleView addSubview:self.selectedLine];
        
        //view
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, titleHeight, ScreenWidth, self.height - titleHeight)];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces = NO;
        self.scrollView.delegate = self;
        [self addSubview:self.scrollView];
        
        self.delegate = delegate;
        self.ctlArray = ctlArray;
        //视图
        self.ctlArray = ctlArray;
        for (UIViewController *vc in self.ctlArray){
            [vc.view removeFromSuperview];
            [vc removeFromParentViewController];
        }
        
        for (int i = 0; i<self.ctlArray.count; i++){
            UIViewController *vc = self.ctlArray[i];
            [(UIViewController *)_delegate addChildViewController:vc];
            vc.view.height = _scrollView.height;
            vc.view.left = ScreenWidth * i;
            [_scrollView addSubview:vc.view];
        }
        
        _scrollView.contentSize = CGSizeMake(ScreenWidth * self.ctlArray.count, _scrollView.height);
    }
    return self;
}

#pragma mark事件
/**点击按钮*/
-(void)btnClick:(UIButton *)btn{
    [self toPage:btn.tag];
}

// 跳转指定页面 0 开始
- (void)toPage:(NSInteger)page{
    [_scrollView setContentOffset:CGPointMake(_scrollView.width * page, 0) animated:YES];
    self.selectedLine.left = _scrollView.contentOffset.x/(_scrollView.width/self.btWidth);
    [self setSeletedBtAndSeletedView:page];
}

-(void)setSeletedBtAndSeletedView:(NSInteger)page{
    //第一次是由选中颜色变为未选中颜色，第二次是将未选中颜色变为选择颜色
    [self.seletBt setTitleColor:self.titleNormalColor forState:UIControlStateNormal];
    self.seletBt = self.btArray[page];
    [self.seletBt setTitleColor:self.titleSeletedColor forState:UIControlStateNormal];
    
    if (_delegate && [_delegate respondsToSelector:@selector(viewDidEndDecelerating:viewController:)] && self.ctlArray.count>page){
        [_delegate viewDidEndDecelerating:page viewController:self.ctlArray[page]];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat floatScrollView = _scrollView.width;
    
    self.selectedLine.left = _scrollView.contentOffset.x / (floatScrollView/self.btWidth);
}

/**滚动*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger offset = (int)_scrollView.contentOffset.x/(int)ScreenWidth;
    [self setSeletedBtAndSeletedView:offset];
}

@end
